package at.fhj.itm11.forum.beans;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.http.HttpSession;

import at.fhj.itm11.forum.data.UserDAO;
import at.fhj.itm11.forum.service.CommentService;
import at.fhj.itm11.forum.service.dtos.CommentDTO;
import at.fhj.itm11.forum.service.dtos.PostDTO;
import at.fhj.itm11.forum.service.dtos.UserDTO;
import at.fhj.itm11.forum.util.ForumConstants;

@ManagedBean
@RequestScoped
public class CommentsBean implements Serializable {

	private static final long serialVersionUID = -5203189585531897908L;

	@Inject
	private AuthenticationBean authBean;

	@Inject
	@Named("commentServiceImpl")
	private CommentService commentService;
	
	@Inject
	@Named("userDAOImpl")
	private UserDAO userDao;

	private CommentDTO comment;

	private UserDTO user;
	private PostDTO post;

	private HttpSession session;

	public CommentDTO getComment() {
		return comment;
	}

	public void setComment(CommentDTO comment) {
		this.comment = comment;
	}

	@PostConstruct
	public void initcomment() {
		comment = new CommentDTO();
		session = (HttpSession) authBean.getSession();
	}

	public List<CommentDTO> loadComments(PostDTO post) {
		this.setPost(post);
		user = (UserDTO) authBean.getSession().getAttribute(
				ForumConstants.SESSION_CURRENT_USER);
		return commentService.findCommentsByPostOrderedByDate(post.getId());
	}

	public UserDTO getUser() {
		return user;
	}

	public void setUser(UserDTO user) {
		this.user = user;
	}

	public PostDTO getPost() {
		return post;
	}

	public void setPost(PostDTO post) {
		this.post = post;
	}

	private String getRootErrorMessage(Exception e) {
		// Default to general error message that registration failed.
		String errorMessage = "Registration failed. See server log for more information";
		if (e == null) {
			// This shouldn't happen, but return the default messages
			return errorMessage;
		}

		// Start with the exception and recurse to find the root cause
		Throwable t = e;
		while (t != null) {
			// Get the message from the Throwable class instance
			errorMessage = t.getLocalizedMessage();
			t = t.getCause();
		}
		// This is the root cause message
		return errorMessage;
	}

	public void register() {
		FacesContext context = FacesContext.getCurrentInstance();
		try {

			if (comment.getText() == null || "".equals(comment.getText())) {
				context.addMessage("reg:text", new FacesMessage(
						FacesMessage.SEVERITY_ERROR, "Creation failed",
						"Text must not be empty!"));
			} else {
				comment.setDate(new Date());
				PostDTO post = (PostDTO) session
						.getAttribute(ForumConstants.SESSION_CURRENT_POST);
				comment.setPost(post);
				comment.setUser(userDao.findById(authBean.getCurrentUser().getId()));
				commentService.register(comment);
				comment = null;
			}

		} catch (Exception e) {
			String errorMessage = getRootErrorMessage(e);
			context.addMessage(null, new FacesMessage(
					FacesMessage.SEVERITY_ERROR, errorMessage,
					"Creation unsuccessful"));
		}
	}

	public void savePostToSession(PostDTO p) {
		session.setAttribute(ForumConstants.SESSION_CURRENT_POST, p);
	}

}
